from celery import Celery

from app.core.config.settings import settings

# 创建Celery实例
celery_app = Celery(
    "disaster_recovery",
    broker=settings.celery_broker_url,
    backend=settings.celery_result_backend,
    include=[
        "app.tasks.health_check_tasks",
        "app.tasks.switch_tasks",
        "app.tasks.notification_tasks"
    ]
)

# 配置Celery
celery_app.conf.update(
    task_serializer="json",
    accept_content=["json"],
    result_serializer="json",
    timezone="UTC",
    enable_utc=True,
    task_track_started=True,
    task_time_limit=30 * 60,  # 30分钟
    task_soft_time_limit=25 * 60,  # 25分钟
    worker_prefetch_multiplier=1,
    worker_max_tasks_per_child=1000,
    beat_schedule={
        "health-check-all": {
            "task": "app.tasks.health_check_tasks.check_all_services",
            "schedule": 30.0,  # 每30秒
        },
        "cleanup-old-logs": {
            "task": "app.tasks.cleanup_tasks.cleanup_old_logs",
            "schedule": 3600.0,  # 每小时
        },
    }
)